package com.ifudata.dataexpert.model;

import java.util.HashMap;

import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.SecurityUtils;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ifudata.dataexpert.constants.SmsConstants;
import com.ifudata.dataexpert.util.IpUtil;
import com.ifudata.dataexpert.util.JWTUtil;
import com.ifudata.dvp.base.exception.BusinessException;
import com.ifudata.dvp.base.vo.RequestHeader;
import com.ifudata.dvp.sdk.util.RedisUtil;
import com.ifudata.dvp.sdk.util.StringUtil;
import com.ifudata.dvp.sys.api.staff.param.StaffQueryResponse;
import com.ifudata.dvp.sys.api.staffintegrity.param.DepartVo;
import com.ifudata.dvp.sys.api.staffintegrity.param.StaffIntegrityQueryResponse;
import com.ifudata.dvp.sys.api.staffintegrity.param.StaffIntegrityVo;
import com.ifudata.dvp.sys.api.staffintegrity.param.StaffVo;

/**
 * @Description 接口返回对象
 * @Date 2018-03-17
 * @Time 22:25
 */
@Component
public class ResultMap extends HashMap<String, Object> {

	private static final long serialVersionUID = 1L;
	
	/**
	 * 需要返回新token
	 * @param isToken
	 */
	public ResultMap(boolean isToken) {
		//刷新token
		if( isToken ){
			this.put("token", JWTUtil.createToken(JWTUtil.getUserInfo(SecurityUtils.getSubject().getPrincipal().toString())));			
		}
    }
	
	public ResultMap() {
		
    }
	
    public ResultMap success() {
        this.put("code", SmsConstants.ResCode.CODE_200);
        return this;
    }

    public ResultMap fail() {
        this.put("code", SmsConstants.ResCode.CODE_400);
        return this;
    }
    
    public ResultMap exception() {
        this.put("code", SmsConstants.ResCode.CODE_500);
        return this;
    }

    public ResultMap code(int code) {
        this.put("code", code);
        return this;
    }
    
    public ResultMap token(String token) {
    	this.put("token", token);
        return this;
    }

    public ResultMap message(Object message) {
        this.put("message", message);
        return this;
    }
    
    public ResultMap dataJson(JSONObject json) {
        this.put("data", json);
        return this;
    }
    
    /**
     * 获取用户登录名称
     * @return
     */
    @Deprecated
    public String getLoginCode() {
    	String loginCode = null;
        String userInfo = JWTUtil.getUserInfo(SecurityUtils.getSubject().getPrincipal().toString());
        if(!StringUtil.isBlank(userInfo)){
        	StaffQueryResponse user = JSON.parseObject(userInfo, StaffQueryResponse.class);
        	loginCode = user.getStaffNo();
        }
        return loginCode;
    }
    
    /**
     * 获取用户信息
     * @return
     */
    public StaffQueryResponse getStaff() {
    	StaffQueryResponse user = null;
        String userInfo = JWTUtil.getUserInfo(SecurityUtils.getSubject().getPrincipal().toString());
        if(!StringUtil.isBlank(userInfo)){
        	user = JSON.parseObject(userInfo, StaffQueryResponse.class);
        }
        return user;
    }
    
    /**
     * 封装RequestHeader
     * @param request
     * @return
     */
    public RequestHeader getRequestHeader(HttpServletRequest request) {
    	String userInfo = JWTUtil.getUserInfo(SecurityUtils.getSubject().getPrincipal().toString());
    	if( StringUtil.isBlank(userInfo) ) throw new BusinessException("获取不到用户信息");
        StaffQueryResponse user = JSON.parseObject(userInfo, StaffQueryResponse.class);
        
        String privileged = RedisUtil.get("USERPRIVILEGED_" + user.getStaffNo());
        if( StringUtil.isBlank(privileged) ) throw new BusinessException("获取不到用户权限信息");
        StaffIntegrityQueryResponse userPrivileged = JSON.parseObject(privileged, StaffIntegrityQueryResponse.class);
       
		StaffIntegrityVo staffIntegrityVo = userPrivileged.getStaffIntegrityVo();
		StaffVo staffvo = staffIntegrityVo.getStaffVo();
		DepartVo deptVo = staffIntegrityVo.getDepartVo();
		RequestHeader requestHeader = new RequestHeader();
		requestHeader.setClientIp(IpUtil.getIpAddr(request));
		requestHeader.setStaffId(staffvo.getStaffId());
		requestHeader.setCityCode(deptVo.getCityCode());
		requestHeader.setDepartId(deptVo.getDepartId());
		requestHeader.setDepartLevel(deptVo.getDepartLevel());
		requestHeader.setProvinceCode(deptVo.getProvinceCode());

		return requestHeader;

	}
    
}

